package com.hiultra.archive.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.domain.ArchiveBoxStatus;
import com.hiultra.archive.domain.vo.ArchiveBorrowTaskReportVo;
import com.hiultra.archive.domain.vo.ArchiveRoomAlarmReportVo;
import com.hiultra.archive.domain.vo.ResultEntity;
import com.hiultra.archive.utils.tool.ToolUtils;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.mapper.ArchiveRoomAlarmMapper;
import com.hiultra.archive.domain.ArchiveRoomAlarm;
import com.hiultra.archive.service.IArchiveRoomAlarmService;

/**
 * 档案室环境预警信息Service业务层处理
 *
 * @author paidaxing
 * @date 2024-07-29
 */
@Service
public class ArchiveRoomAlarmServiceImpl implements IArchiveRoomAlarmService {
    @Autowired
    private ArchiveRoomAlarmMapper archiveRoomAlarmMapper;
    @Value("${pubKey}")
    private String pubKey;

    @Value("${priKey}")
    private String priKey;
    @Value("${urlIp}")
    private String urlIp;
    @Value("${archiveRoomAlarmRecordUrl}")
    private String archiveRoomAlarmRecordUrl;

    /**
     * 查询档案室环境预警信息
     *
     * @param id 档案室环境预警信息主键
     * @return 档案室环境预警信息
     */
    @Override
    public ArchiveRoomAlarm selectArchiveRoomAlarmById(Long id) {
        return archiveRoomAlarmMapper.selectArchiveRoomAlarmById(id);
    }

    /**
     * 查询档案室环境预警信息列表
     *
     * @param archiveRoomAlarm 档案室环境预警信息
     * @return 档案室环境预警信息
     */
    @Override
    public List<ArchiveRoomAlarm> selectArchiveRoomAlarmList(ArchiveRoomAlarm archiveRoomAlarm) {
        return archiveRoomAlarmMapper.selectArchiveRoomAlarmList(archiveRoomAlarm);
    }

    /**
     * 新增档案室环境预警信息
     *
     * @param archiveRoomAlarm 档案室环境预警信息
     * @return 结果
     */
    @Override
    public int insertArchiveRoomAlarm(ArchiveRoomAlarm archiveRoomAlarm) {
        return archiveRoomAlarmMapper.insertArchiveRoomAlarm(archiveRoomAlarm);
    }

    /**
     * 修改档案室环境预警信息
     *
     * @param archiveRoomAlarm 档案室环境预警信息
     * @return 结果
     */
    @Override
    public int updateArchiveRoomAlarm(ArchiveRoomAlarm archiveRoomAlarm) {
        return archiveRoomAlarmMapper.updateArchiveRoomAlarm(archiveRoomAlarm);
    }

    /**
     * 批量删除档案室环境预警信息
     *
     * @param ids 需要删除的档案室环境预警信息主键
     * @return 结果
     */
    @Override
    public int deleteArchiveRoomAlarmByIds(Long[] ids) {
        return archiveRoomAlarmMapper.deleteArchiveRoomAlarmByIds(ids);
    }

    /**
     * 删除档案室环境预警信息信息
     *
     * @param id 档案室环境预警信息主键
     * @return 结果
     */
    @Override
    public int deleteArchiveRoomAlarmById(Long id) {
        return archiveRoomAlarmMapper.deleteArchiveRoomAlarmById(id);
    }

    /**
     * 档案室环境预警信息上报
     *
     * @param archiveRoomAlarm 档案室环境预警信息
     * @return 结果
     */
    @Override
    public int reportArchiveRoomAlarm(ArchiveRoomAlarm archiveRoomAlarm) {
        //转化上报实体
        ArchiveRoomAlarmReportVo archiveRoomAlarmReportVo = ToolUtils.convertEntity(archiveRoomAlarm, ArchiveRoomAlarmReportVo::new);
        if (ObjectUtil.isNotNull(archiveRoomAlarm.getAlarmTime())) {
            archiveRoomAlarmReportVo.setAlarmTime(DateUtil.format(archiveRoomAlarm.getAlarmTime(), "yyyyMMddHHmmss"));
        }
        String jsonString = "";
        // 将JsonData对象转换为JSON字符串
        try {
            jsonString = new ObjectMapper().writeValueAsString(archiveRoomAlarmReportVo);
            System.out.println("档案室环境预警信息上报" + jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("档案室环境预警信息上报url" + archiveRoomAlarmRecordUrl);
        //发送请求获取数据
        HashMap hashMap = ToolUtils.sendPostRequest(BusinessConstants.ARCHIVE_ROOM_ALARM_REPORT_NO, urlIp, archiveRoomAlarmRecordUrl, "档案室环境预警信息上报", jsonString, pubKey, priKey);
        System.out.println("档案室环境预警信息上报" + hashMap.toString());
        if (hashMap.get("code").equals("00000")) {
            archiveRoomAlarm.setPushStatus(BusinessConstants.END_PUSHSTATUS);
            archiveRoomAlarmMapper.updateArchiveRoomAlarm(archiveRoomAlarm);
            System.out.println("档案室环境预警信息上报成功,推送状态修改为已推送！");
        } else {
            System.out.println("档案室环境预警信息上报失败");
        }

        return 0;
    }

    /**
     * 档案室环境预警信息定时上报接口
     */
    @Override
    public ResultEntity timingArchiveRoomAlarmResultReport() {
        List<ArchiveRoomAlarm> archiveRoomAlarmList = archiveRoomAlarmMapper.selectArchiveRoomAlarmList(new ArchiveRoomAlarm() {{
            setPushStatus(BusinessConstants.PUSHSTATUS);
        }});

        if (archiveRoomAlarmList.size() > 0) {
            for (ArchiveRoomAlarm archiveRoomAlarm : archiveRoomAlarmList) {
                reportArchiveRoomAlarm(archiveRoomAlarm);
            }
        }
        return ResultEntity.success();
    }
}
